/*
 * OptionDialog.java
 *
 * Created on August 27, 2008, 11:16 PM
 */

package xq.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import xq.swing.InputPanel.Message;

/**
 *
 * @author  ThorntonRP
 */
public class OptionDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = -7246835043804982065L;
	
	public static final int CANCEL_OPTION = 0;
	public static final int APPROVE_OPTION = 1;
	
	private InputPanel optionComponent;
	private int option;
	
    public OptionDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, modal);
		setTitle(title);
        initComponents();
    }

	public void setOptionComponent(InputPanel c) {
		if (this.optionComponent != null) {
			remove(this.optionComponent);
		}
		this.optionComponent = c;
		add(c, BorderLayout.CENTER);
		c.addPropertyChangeListener("messages", new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				updateMessagePanel();
			}
		});
		updateMessagePanel();
	}
	
	public InputPanel getOptionComponent() {
		return optionComponent;
	}
	
	public int getOption() {
		return option;
	}
	
	private void updateMessagePanel() {
		List<Message> messages = optionComponent.getMessages();
		messagePanel.removeAll();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		for (int i = 0, size = messages.size(); i < size; i++) {
			Message msg = messages.get(i);
			StringWriter msgText = new StringWriter();
			msgText.append("<html>").append(msg.getMessage().replaceAll("(\r)?\n", "<br>"));
			if (msg.getThrown() != null) {
				PrintWriter pw = new PrintWriter(msgText);
				pw.print("<pre>");
				msg.getThrown().printStackTrace(pw);
			}
			JLabel msgLabel = new JLabel(msgText.toString());
			msgLabel.setBorder(new TitledBorder(msg.getComponentName()));
			switch (msg.getType()) {
				case ERROR: msgLabel.setForeground(Color.RED); break;
				case WARNING: msgLabel.setForeground(Color.ORANGE); break;
				case INFORMATION: default: msgLabel.setForeground(Color.BLACK); break;
			}
			messagePanel.add(msgLabel, gbc);
		}
		pack();
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JPanel footerPane = new javax.swing.JPanel();
        messagePanel = new javax.swing.JPanel();
        javax.swing.JButton approveButton = new javax.swing.JButton();
        javax.swing.JButton cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        messagePanel.setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("xq/swing/Bundle"); // NOI18N
        approveButton.setText(bundle.getString("OptionDialog.approveButton.text")); // NOI18N
        approveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                approveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(bundle.getString("OptionDialog.cancelButton.text")); // NOI18N
        cancelButton.setDefaultCapable(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout footerPaneLayout = new javax.swing.GroupLayout(footerPane);
        footerPane.setLayout(footerPaneLayout);
        footerPaneLayout.setHorizontalGroup(
            footerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(approveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
        );

        footerPaneLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {approveButton, cancelButton});

        footerPaneLayout.setVerticalGroup(
            footerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(footerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(messagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addGroup(footerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cancelButton)
                        .addComponent(approveButton)))
                .addContainerGap())
        );

        getContentPane().add(footerPane, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void approveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_approveButtonActionPerformed
	option = APPROVE_OPTION;
	setVisible(false);
}//GEN-LAST:event_approveButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
	setVisible(false);
}//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel messagePanel;
    // End of variables declaration//GEN-END:variables

}
